﻿'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports System.Collections.Specialized
Imports System.Web
Imports WillStrohl.Modules.CodeCamp.QueryStrings

Namespace WillStrohl.Modules.CodeCamp

    Public Class CodeCampHandler
        Implements System.Web.IHttpHandler

        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

            context.Response.ContentType = "text/plain"

            If Me.IsRequestValid(context.Request.QueryString) Then
                Try
                    context.Response.Write(SaveCodeCamp(context.Request.QueryString))
                Catch ex As Exception
                    DotNetNuke.Services.Exceptions.LogException(ex)
                    context.Response.Write("-1")
                End Try
            Else
                context.Response.Write("-1")
            End If

        End Sub

        Private Function SaveCodeCamp(ByVal QueryString As NameValueCollection) As String

            Dim intCodeCamp As Integer = Null.NullInteger
            Dim ctlCodeCamp As New CodeCampController
            Dim objCodeCamp As New CodeCampInfo

            Try

                If Not QueryString(CODE_CAMP_ID) Is Nothing Then
                    intCodeCamp = Integer.Parse(QueryString(CODE_CAMP_ID).ToString, Globalization.NumberStyles.Integer)
                    objCodeCamp = ctlCodeCamp.GetCodeCampById(intCodeCamp)
                End If

                With objCodeCamp

                    ' assign required fields
                    .BeginDate = DateTime.Parse(QueryString(BEGIN_DATE).ToString)
                    .CreatedByUser = Integer.Parse(QueryString(CREATED_BY_USER).ToString, Globalization.NumberStyles.Integer)
                    .Description = QueryString(DESCRIPTION).ToString
                    .EndDate = DateTime.Parse(QueryString(END_DATE).ToString)
                    .MaximumCapacity = Integer.Parse(QueryString(MAXIMUM_CAPACITY).ToString, Globalization.NumberStyles.Integer)
                    .ModuleId = Integer.Parse(QueryString(MODULE_ID).ToString, Globalization.NumberStyles.Integer)
                    .RegistrationActive = Boolean.Parse(QueryString(REGISTRATION_ACTIVE).ToString)
                    .RoleName = QueryString(ROLE_NAME).ToString
                    .ShowAuthor = Boolean.Parse(QueryString(SHOW_AUTHOR).ToString)
                    .ShowMicrosoftMVP = Boolean.Parse(QueryString(SHOW_MICROSOFT_MVP).ToString)
                    .ShowShirtSize = Boolean.Parse(QueryString(SHOW_SHIRT_SIZE).ToString)
                    .Title = QueryString(TITLE).ToString
                    .WaitListRoleName = QueryString(WAIT_LIST_ROLE_NAME).ToString

                    If .CodeCampId > Null.NullInteger Then
                        ctlCodeCamp.UpdateCodeCamp(intCodeCamp, .ModuleId, .Title, .Description, .BeginDate, _
                            .EndDate, .IconFile, .AltText, .RoleName, .WaitListRoleName, .MaximumCapacity, _
                            .NumberRegistered, .EventFull, .RegistrationActive, .ShowShirtSize, .ShowMicrosoftMVP, _
                            .ShowAuthor, .CreatedByUser)
                    Else
                        intCodeCamp = ctlCodeCamp.AddCodeCamp(.ModuleId, .Title, .Description, .BeginDate, .EndDate, _
                            String.Empty, String.Empty, .RoleName, .WaitListRoleName, .MaximumCapacity, _
                            .RegistrationActive, .ShowShirtSize, .ShowMicrosoftMVP, .ShowAuthor, .CreatedByUser)
                    End If

                End With

            Catch e As Exception

                DotNetNuke.Services.Exceptions.LogException(e)

            End Try

            Return intCodeCamp.ToString

        End Function

        Private Function IsRequestValid(ByVal QueryString As NameValueCollection) As Boolean

            Return (Not QueryString(MODULE_ID) Is Nothing And Not QueryString(TITLE) Is Nothing _
                And Not QueryString(DESCRIPTION) Is Nothing And Not QueryString(BEGIN_DATE) Is Nothing _
                And Not QueryString(END_DATE) Is Nothing And Not QueryString(ROLE_NAME) Is Nothing _
                And Not QueryString(WAIT_LIST_ROLE_NAME) Is Nothing And Not QueryString(MAXIMUM_CAPACITY) Is Nothing _
                And Not QueryString(SHOW_SHIRT_SIZE) Is Nothing And Not QueryString(SHOW_MICROSOFT_MVP) Is Nothing _
                And Not QueryString(SHOW_AUTHOR) Is Nothing And Not QueryString(CREATED_BY_USER) Is Nothing _
                And Not QueryString(REGISTRATION_ACTIVE) Is Nothing)

        End Function

        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

    End Class

End Namespace